<?php

/**
 * Class for Exception managing and debugging
 * @author Francesco
 *
 */
class ExceptionHandler {
	
	/**
	 * Code of the current Exception
	 * @var string
	 */
	public $code;
	
	/**
	 * File that generated the current exception
	 * @var string
	 */
	public $file;
	
	/**
	 * Exception message 
	 * @var string
	 */
	public $message;
	
	/**
	 * File line where exception has been throwed
	 * @var int
	 */
	public $line;
	
	/**
	 * Method called before the exception occurence
	 * @var string
	 */
	public $previous;
	
	/**
	 * Php stack trace, for actions maping
	 * @var string
	 */
	public $trace;
	
	/**
	 * String conversion of the exception
	 * @var string
	 */
	public $_toString;
	
	private $log_file;
	private $log_dir;
		
	/**
	 * Contructor
	 * @param Exception $e
	 */
	function __construct(Exception $e){
		$this->code = $e->getCode();
		$this->message = $e->getMessage();
		$this->file = $e->getFile();
		$this->line = $e->getLine();
		$this->previous = $e->getPrevious();
		$this->trace = $e->getTrace();
		$this->string = $e->__toString();
		//If firebug debug is enabled, we use this cool tool!
		if(FIREBUG_DEBUG){
			$this->fireLog();
		}
		if(FILE_DEBUG){
			$this->fileLog();
		}
	}
	
	/**
	 * Log the class vars into firebug... Very very usefull!!!!
	 */
	private function fireLog(){
		fb::log($this->code, 'code');
		fb::log($this->message, 'message');
		fb::log($this->file, 'file');
		fb::log($this->line, 'line');
		fb::log($this->previous, 'previous');
		fb::log($this->trace, 'trace');
		fb::log($this->string, 'string');
	}
	
	/**
	 * TODO : file and db logging
	 */
	private function fileLog(){
		if(is_defined("LOG_FILE")){
			$this->log_file = LOG_FILE;
		} else {
			$this->log_file = "log.txt";
		}
		self::write_log($this->code, 'code');
		self::write_log($this->message, 'message');
		self::write_log($this->file, 'file');
		self::write_log($this->line, 'line');
		self::write_log($this->previous, 'previous');
		self::write_log($this->trace, 'trace');
		self::write_log($this->string, 'string');
		self::write_log();
	}
	
	private function write_log($text = '', $title = ''){
		
	}
	
}